﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TwoWheels.Account;
using TwoWheels.Order;

namespace TwoWheels.Order
{
    public partial class Confirmation : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                OrderView.Columns.Clear();              

                const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
                TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);

                UserShoppingCart userCart = (Session["UserShoppingCartInfo"] as UserShoppingCart);

                Dictionary<int, int> product = userCart.Products;

                DataTable tbl = new DataTable();
                tbl.Columns.Add(new DataColumn("Product Name"));
                tbl.Columns.Add(new DataColumn("Price"));
                tbl.Columns.Add(new DataColumn("Quantity"));

                float totalPrice = 0;

                int i = 0;
                foreach (int productID in product.Keys)
                {
                    var pr = from p in tdb.products where p.ProduktID == productID select new { p.NazwaProduktu, p.CenaJednostkowa };
                    DataRow dr = tbl.DefaultView.Table.NewRow();
                    dr["Product Name"] = (from n in pr select n.NazwaProduktu).First().ToString();
                    float price = (float) (from n in pr select n.CenaJednostkowa).First();
                    dr["Price"] = price; //dodac znizki, precyzja
                    totalPrice += (price*product[productID]);
                    dr["Quantity"] = product[productID];

                    tbl.Rows.InsertAt(dr, i++);
                }

                TotalPrice.Text = totalPrice.ToString();

                OrderView.DataSource = tbl;
                OrderView.DataBind();
                
                var clientInfo = from c in tdb.clients where c.KlientID == userCart.Id select new { Name = c.Imie, Surname = c.Nazwisko, 
                                                                                                    Address = (c.Adres == null ? "":c.Adres),
                                                                                                    City = (c.Miasto == null ? "" : c.Miasto),
                                                                                                    Country = (c.Panstwo == null ? "" : c.Panstwo),
                                                                                                    Telephone = c.Telefon == null ? "":c.Telefon,
                                                                                                    Code = c.KodPocztowy==null ? "":c.KodPocztowy, 
                                                                                                    Company = c.NazwaFirmy==null ? "": c.NazwaFirmy,
                                                                                                    NIP = c.NIP==null ? "":c.NIP};
               
                Name.Text = (from c in clientInfo select c.Name).First().ToString() ;
                Surname.Text = (from c in clientInfo select c.Surname).First().ToString();

                Address.Text = (from c in clientInfo select c.Address).First().ToString();
                City.Text = (from c in clientInfo select c.City).First().ToString();
                Country.Text = (from c in clientInfo select c.Country).First().ToString();
                Tel.Text = (from c in clientInfo select c.Telephone).First().ToString();
                Code.Text = (from c in clientInfo select c.Code).First().ToString();
                CompanyNameTextBox.Text = (from c in clientInfo select c.Company).First().ToString();
                NIPTextBox.Text = (from c in clientInfo select c.NIP).First().ToString();


            }
        }

        protected void goBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(""); // zakolejkuj strone na page load z ktorej przyszedlem i tu ja pobierz
        }


        protected void onCheckChanged(object sender, EventArgs e)
        {
            CompanyNameTextBox.Enabled = true;
            CompanyNameTextBox.BackColor = System.Drawing.Color.White;

            NIPTextBox.Enabled = true;
            NIPTextBox.BackColor = System.Drawing.Color.White;
        }

        protected void ConfirmOrder_Click(object sender, EventArgs e)
        {
            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);

            String supplierName = DropDownList1.SelectedItem.Text;
            int supplierID = (from s in tdb.suppliers where s.Nazwa.Equals(supplierName) select s.DostawcaID).First();
            int klientID = (Session["UserShoppingCartInfo"] as UserShoppingCart).Id;
            DateTime orderDate = DateTime.Now;
            String shipTo = Name.Text + " " + Surname.Text + " " + Address.Text + " " + Code.Text;
            String cityTo = City.Text;
            String countryTo = Country.Text;

            Zamówienie zamowienie = new Zamówienie {AdresMiejscaDostarczenia = shipTo, KlienciKlientID = klientID, DostawcyDostawcaID = supplierID, DataZamówienia = orderDate, MiastoMiejscaDostarczenia = cityTo, PaństwoMiejscaDostarczenia = countryTo };
       
            //nie trzeba tak sprawdzac, dopoki nie dostanie submita to nie bedzie utrwalone

            foreach(KeyValuePair<int,int> entry in (Session["UserShoppingCartInfo"] as UserShoppingCart).Products)
            {
                if (tdb.addProcedure.CheckState(entry.Key) < entry.Value)
                {
                    Response.Redirect("~/Order/Outof.aspx");
                }
                
            }
            foreach (KeyValuePair<int, int> entry in (Session["UserShoppingCartInfo"] as UserShoppingCart).Products)
            {
                if (tdb.addProcedure.DecProduct(entry.Key,entry.Value) == -1)
                {
                    Response.Redirect("~/Order/Outof.aspx");
                }
            }

            tdb.order.InsertOnSubmit(zamowienie);
            tdb.SubmitChanges();

            (Session["UserShoppingCartInfo"] as UserShoppingCart).Products.Clear();
            
            Response.Redirect("~/Default.aspx");
        }
    }
}